﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;
using System.Configuration;

/// <summary>
/// Summary description for WarrantyDBConnection
/// </summary>
public class WarrantyDBConnection
{
    OleDbConnection conn;
    OleDbCommand cmd;
    OleDbDataAdapter datatAdapter;
    OleDbDataReader dataReader;
    public String connString = "";

	public WarrantyDBConnection()
    {
        conn = new OleDbConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["AssetDbConnection"].ConnectionString;
        //cmd = new OleDbCommand();
	}

    public Warranty getWarrantyById(int warrantyID)
    {
        connString = "SELECT * FROM Warranty WHERE Warranty_ID = @Warranty";
        cmd = conn.CreateCommand();
        cmd.CommandText = connString;

        conn.Open();

        OleDbParameter computerIDParam = new OleDbParameter();
        computerIDParam.ParameterName = "@Warranty";
        computerIDParam.OleDbType = OleDbType.Numeric;
        computerIDParam.Size = 255;
        computerIDParam.Direction = ParameterDirection.Input;
        computerIDParam.Value = warrantyID;

        cmd.Parameters.Add(computerIDParam);

        //myOleDbCommand.Connection.Open();
        dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        Warranty warranty = new Warranty();
        while (dataReader.Read())
        {
            warranty.warranty_ID = Int32.Parse(dataReader["Warranty_ID"].ToString());
            warranty.provider_Info = dataReader["Provider_Info"].ToString();
            warranty.warranty_Status = dataReader["Warranty_Status"].ToString();
            warranty.start_Date = DateTime.Parse(dataReader["Start_Date"].ToString());
            warranty.expire_Date = DateTime.Parse(dataReader["Expire_Date"].ToString());
        }

        closeObjects();

        return warranty;
    }

    public void update(Computer computer)//Not done yet.
    {
        String connStringUpdate = "UPDATE Computers SET Computer_Name = '" + computer.computer_Name + "' WHERE Computer_ID = " + computer.computer_ID + ";";

        cmd = new OleDbCommand(connStringUpdate, conn);
        conn.Open();

        cmd.ExecuteNonQuery();
        conn.Close();

    }

    public void updateAll(Warranty warranty)//Not done yet.
    {
        String connStringUpdate = "UPDATE Computers SET Start_Date = '"+warranty.start_Date
                                 + "', Warranty_Status = '" + warranty.warranty_Status 
                                 + "', Provider_Info = '" + warranty.provider_Info
                                 + "', Expire_Date = '" + warranty.expire_Date
                                 + "' WHERE Warranty_ID = " + warranty.warranty_ID + ";";

        cmd = new OleDbCommand(connStringUpdate, conn);
        conn.Open();

        cmd.ExecuteNonQuery();
        closeObjects();
    }

    public void delete(Software software)
    {
        connString = "DELETE FROM Software WHERE Software_ID = " + software.software_ID + ";";
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["AssetDbConnection"].ConnectionString;


        cmd = new OleDbCommand(connString, conn);
        conn.Open();

        cmd.ExecuteNonQuery();
        conn.Close();
    }

    public void closeObjects()
    {
        if (conn.State.ToString().Equals("Open"))
        {
            conn.Close();
        }
        if (cmd.Connection.State.ToString().Equals("Open"))
        {
            cmd.Connection.Close();
        }
    }

}